#include "labelmanager.h"

LabelManager::LabelManager()
{
    mode=0;
    scale=1;
    selected=-1;
    last_selected=-1;
    dx=0;
    dy=0;
    start_at=0;
    draw_back=true;
}

int LabelManager::GetItemNumber(){

    return labels.count();
}

void LabelManager::AddLabel(int x, int y){
    labels.append(new CromoLabel(x,y));
}

void LabelManager::AddLabel(int x, int y, int num){
    labels.insert(num, new CromoLabel(x,y));
}

CromoLabel* LabelManager::GetLabel(int num){
    if(num>=0 && num<labels.count()){
        return labels[num];
    }

    return 0;

}

void LabelManager::RemoveLabel(int num){
    if(num>=0 && num<labels.count()){
        delete labels[num];
        labels.remove(num);
    }
}

void LabelManager::Clean(){
    mode=0;
    selected=-1;
    last_selected=-1;
    for(int i=0;i<labels.count();i++){
        delete labels[i];
    }
    labels.clear();
}

void LabelManager::PaintLabels(QImage *image){
    QPainter painter(image);

    QSettings settings("TID","Cromos");
    int r = settings.value("backR",160).toInt();
    int g = settings.value("backG",160).toInt();
    int b = settings.value("backB",160).toInt();

    QBrush nonSel(QColor(r,g,b,160));
    QBrush Sel(QColor(220,0,0,255));
    painter.setPen(Qt::NoPen);

    draw_back = settings.value("draw_back", true).toBool();
    start_at = settings.value("start_at",0).toInt();
    bool show_count = settings.value("show_count",true).toBool();

    for(int i=0; i<labels.count(); i++){
        if(i==selected){
            painter.setBrush(Sel);
            painter.drawRect(labels[i]->x()-2,labels[i]->y()-2,24,24);
        }
        painter.setBrush(nonSel);
        if(draw_back)
            painter.drawRect(labels[i]->x(),labels[i]->y(),20,20);
    }

    r = settings.value("textR",0).toInt();
    g = settings.value("textG",0).toInt();
    b = settings.value("textB",0).toInt();

    QPen penHText(QColor(r,g,b));
    painter.setPen(penHText);
    for(int i=0; i<labels.count(); i++){
        painter.drawText(labels[i]->x(),labels[i]->y(),50,50,0,
                         QString().setNum(i+start_at));
    }
    if(show_count)
        painter.drawText(50,50,250,50,0,QString("Chromosomes labeled: ")+QString::number(labels.size()));
}

void LabelManager::mousePressEvent(QMouseEvent *event){
    if(event->type()== QEvent::MouseButtonDblClick && event->button()==Qt::LeftButton){
        QPoint pos = event->pos();

        if(last_selected==-1){
            AddLabel((pos.x()-10)/scale,(pos.y()-10)/scale);
            selected=labels.count()-1;
        }else{
            AddLabel((pos.x()-10)/scale,(pos.y()-10)/scale,last_selected+1);
            selected=last_selected+1;
        }
        emit Updated();
    }else if(event->type()== QEvent::MouseButtonPress && event->button()==Qt::LeftButton){
        QPoint pos = event->pos();
        last_selected=selected;
        selected=-1;
        for(int i=0; i<labels.count(); i++){
            int lx = labels[i]->x();
            int ly = labels[i]->y();
            lx*=scale;
            ly*=scale;

            if(pos.x()>=lx && pos.x()<lx+20*scale && pos.y()>=ly&& pos.y()<ly+20*scale ){
                selected=i;
                mode=1;
                dx=pos.x()-lx;
                dy=pos.y()-ly;

                break;
            }
        }
        emit Updated();
    }
}

void LabelManager::mouseReleaseEvent(QMouseEvent *event){
    if(event->type()== QEvent::MouseButtonRelease && event->button()==Qt::LeftButton){
        mode=0;
    }
}

void LabelManager::mouseMoveEvent(QMouseEvent *event){
    if(mode==1 && selected>-1){ // Estem arrastrant i hi ha algo seleccionat
        QPoint pos = event->pos();
        labels[selected]->setPos((pos.x()-dx)/scale,(pos.y()-dy)/scale);
        emit Updated();
    }
}

void LabelManager::SetScale(float scale){
    this->scale=scale;
}

int LabelManager::GetSelected(){
    return selected;
}

void LabelManager::RemoveSelectedLabel(){
    if(selected>-1 && mode==0){
        int lx = labels[selected]->x();
        int ly = labels[selected]->y();

        RemoveLabel(selected);
        selected=-1;
        int maxdist=100000;
        for(int i=0; i<labels.count(); i++){
            int lx2 = labels[i]->x();
            int ly2 = labels[i]->y();

            int dist=abs(lx-lx2)+abs(ly-ly2);

            if(dist<maxdist){
                maxdist=dist;
                selected=i;
            }
        }
        emit Updated();


    }
}

void LabelManager::SetSelected(int sel){
    selected=sel;
    emit Updated();
}
